//
//  CheLiangDetailViewController.m
//  ZuLinProject
//
//  Created by 谭明星 on 2021/12/8.
//

#import "CheLiangDetailViewController.h"
#import "CheLiangOperateListCell.h"
#import "LXInsuranceRecordViewController.h" //  保险记录
#import "LXRepairRecordViewController.h" //  维修记录
#import "LXYearCheckRecordViewController.h" //  年检记录
#import "LXMaintainRecordViewController.h" //  保养记录
#import "LXCarLeaseApplyViewController.h"// 车辆上架
#import "LXSignRecordViewController.h"// 签约记录
#import "LXCarEditViewController.h"// 修改车辆
#import "LXPaymentRecordViewController.h"// 付款记录


#import "LXSignContractModel.h"
#import "LXReturnCarViewController.h"
#import "LXSignContractApplyViewController.h"
#import "LXCarDetailsTopView.h"
#import "LXCarSignListViewController.h"
#import "LXDelayRecordViewController.h"
#import "LXCheckRecordViewController.h"
#import "LXAgencyFeeRecordViewController.h"
#import "LXReturnCarRecordViewController.h"
#import "LXInsuranceUseRecordViewController.h"
#import "LXViolationRecordViewController.h"
#import "LXDepositRecordViewController.h"
#import "LXLoanRecordViewController.h"
#import "LXPlateRecordViewController.h"
#import "LXSalaryRecordViewController.h"


#define kItemHeight KHeightScale(95)
#define kItemCount 3
@interface CheLiangDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) LXCarDetailsTopView *topView;
@property (nonatomic, strong) CheLiangModel *carModel;


@end

@implementation CheLiangDetailViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    if (kUserDefaultForBool(kRefreshStr)) {
//        kUserDefaultSetBool(NO, kRefreshStr);
        [self getCarInfo];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = ColorWithF5F5F5;
    
    
    
    [self getCarInfo];
    
    LXCarDetailsTopView *topView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXCarDetailsTopView class]) owner:nil options:nil].firstObject;
    self.topView = topView;
    topView.frame = CGRectMake(0, 0, ScreenWidth, 115 + kViewTopHeight);
    [self.view addSubview:topView];
//    topView.model = self.detailModel;
    [topView.btn5 whenTapped:^{
        LXCarEditViewController *vc = [[LXCarEditViewController alloc] init];
        vc.carModel = self.carModel;
        [self pushToNextController:vc];
    }];
    [topView.btn6 whenTapped:^{
        [self carDeleteAction];
    }];
    
    [topView.lable4 whenTapped:^{
        if ([self.detailModel.hireFlag intValue] == 0) {
            [Tool showSheetTitle:nil msg:nil cancelTitle:@"取消" oneBtnTitle:@"租车" twoBtnTitle:nil cancelHandlerBlock:^{
            } oneHandlerBlock:^{
                LXSignContractApplyViewController *vc = [[LXSignContractApplyViewController alloc] init];
                LXSignContractModel *model = [LXSignContractModel new];
                model.carId = self.detailModel.ID;
                model.carNum = self.detailModel.carNum;
                model.signCarBrand = self.detailModel.carBrand;
                model.signModel = self.detailModel.carMode;
                vc.signContractModel = model;
                vc.type = 1;
                vc.fromVC = NSStringFromClass([self class]);
                [self pushToNextController:vc];
                

            } twoHandlerBlock:^{
            } onVC:self];
        }
        else {
            [Tool showSheetTitle:nil msg:nil cancelTitle:@"取消" oneBtnTitle:@"退车" twoBtnTitle:nil cancelHandlerBlock:^{
            } oneHandlerBlock:^{
                
                [self returnCarAction];
              
            } twoHandlerBlock:^{
            } onVC:self];
        }
        
    }];
    
    
    
    
    [self layoutCustomNavWithWhiteColor:self.view title:@"车辆详情"];

    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake((kScreenWidth - 42)/kItemCount,kItemHeight);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    flowLayout.minimumLineSpacing= 10;
    flowLayout.minimumInteritemSpacing = 10;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, topView.Y_height, ScreenWidth, kScreenHeight - topView.Y_height) collectionViewLayout:flowLayout];
    [self.view addSubview:collectionView];
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = ColorWithF5F5F5;
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CheLiangOperateListCell  class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([CheLiangOperateListCell class])];
    self.collectionView = collectionView;
    
    
    
  
    self.data = [NSMutableArray array];
    
    [MSNetworkManager postRevertUrl:Car_GetDetailIconList params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {


        if (isSuccess) {

            [self.data addObjectsFromArray:[CheLiangModel mj_objectArrayWithKeyValuesArray:resonseObj]];

            [self.collectionView reloadData];

        }

    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    


}
// MARK: 获取车辆详情
-(void)getCarInfo{
    NSString *url = [NSString stringWithFormat:@"%@?id=%@",CarDetail_URL,self.detailModel.ID];

    [MSNetworkManager postRevertUrl:url params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            CheLiangModel *model = [CheLiangModel mj_objectWithKeyValues:resonseObj[@"carInfo"]];
            self.topView.model = model;
            self.carModel = model;
        }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
}

//MARK: 退车
-(void)returnCarAction{


    [MSNetworkManager getRevertUrl:GET_GetReturnCarDetail params:@{@"carId":self.detailModel.ID} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            CheLiangModel *model = [CheLiangModel mj_objectWithKeyValues:resonseObj];
            
            
            LXReturnCarViewController *vc = [[LXReturnCarViewController alloc] init];
            vc.carModel = model;
            vc.fromVc = NSStringFromClass([self class]);
            [self pushToNextController:vc];
             

        }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
    
}

#pragma mark ---- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.data.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CheLiangOperateListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[CheLiangOperateListCell cellID] forIndexPath:indexPath];
    CheLiangModel *model = self.data[indexPath.row];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:[NSString isChinese:model.icon]]];
    cell.titleL.text = model.name;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CheLiangModel *model = self.data[indexPath.row];
    switch (model.menuId) {
        case 10069://  签约记录
        {
            LXCarSignListViewController *vc = [[LXCarSignListViewController alloc] init];
            vc.carNum = self.carModel.carNum;
            [self pushToNextController:vc];
            break;
        
        }
        case 10244:   // 收/付款记录
        {
        
            LXPaymentRecordViewController *vc = [[LXPaymentRecordViewController alloc] init];
            vc.carModel = self.carModel;
            vc.type = 1;
            [self pushToNextController:vc];
            break;
            
    
        }
        case 10319:   // 退押金记录
        {
            
            
            LXDepositRecordViewController *vc = [[LXDepositRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
       
        }
            break;
        case 10141://维修记录
        {
            
            LXRepairRecordViewController *vc = [[LXRepairRecordViewController alloc] init];
            vc.carModel = self.carModel;
            vc.type = 1;
            [self pushToNextController:vc];
            break;
            
          
        }
        case 10284://延期记录
        {
            LXDelayRecordViewController *vc = [[LXDelayRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
            
        case 10136://保养记录
        {
            
            LXMaintainRecordViewController *vc = [[LXMaintainRecordViewController alloc] init];
            vc.carModel = self.carModel;
            vc.type = 1;
            [self pushToNextController:vc];
            break;
            
      
        }
        case 10304://出险记录
        {
            LXInsuranceUseRecordViewController *vc = [[LXInsuranceUseRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 10146://年检记录
        {
        
            
            LXYearCheckRecordViewController *vc = [[LXYearCheckRecordViewController alloc] init];
            vc.carModel = self.carModel;
            vc.type = 1;
            [self pushToNextController:vc];
            break;
            
        }
        case 10131://保单记录
        {
    
            LXInsuranceRecordViewController *vc = [[LXInsuranceRecordViewController alloc] init];
            vc.carModel = self.carModel;
            vc.type = 1;
            [self pushToNextController:vc];
            break;
            
        }
        case 10289://验车记录
        {
    
            LXCheckRecordViewController *vc = [[LXCheckRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
            
        }
        case 10279://退车记录
        {
            LXReturnCarRecordViewController *vc = [[LXReturnCarRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
            
        }
        case 10314://平台返佣记录
        {
            LXSalaryRecordViewController *vc = [[LXSalaryRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
            
        }
        case 10294://中介费
        {
            LXAgencyFeeRecordViewController *vc = [[LXAgencyFeeRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
            
        }
        case 10059://违约记录
        {
            LXViolationRecordViewController *vc = [[LXViolationRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
            
        }
        case 10269://车贷记录
        {
            
            LXLoanRecordViewController *vc = [[LXLoanRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
            
        }
        case 10309://牌照费记录
        {
            
            LXPlateRecordViewController *vc = [[LXPlateRecordViewController alloc] init];
            vc.type = 1;
            vc.carModel = self.carModel;
            [self.navigationController pushViewController:vc animated:YES];
            break;
            
        }
        default:
            showAlert(@"此功能暂未开放");
            break;
    }
}


#pragma mark -- 车辆删除
- (void)carDeleteAction {
    
   
    
    [Tool showAlertTitle:@"温馨提示" msg:@"是否确认删除当前车辆?" cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        NSDictionary *dic = @{
            @"id":self.detailModel.ID
        };
        [MSNetworkManager postRevertUrl:CarDelete_URL params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            if (isSuccess) {
                kUserDefaultSetBool(YES, kRefreshStr);
                showAlert(@"车辆删除成功!");
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
          
        } withShowHud:YES];
    } onVC:self];

    
    
}

@end
